package com.higold.cloud.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import com.gyf.immersionbar.ktx.immersionBar
import com.higold.cloud.R
import com.higold.cloud.base.BaseFragment
import com.higold.cloud.mvp.contract.UserInfoContract
import com.higold.cloud.mvp.model.bean.userInfo.UserInfo
import com.higold.cloud.mvp.presenter.UserInfoPresenter
import com.higold.cloud.net.exception.ErrorStatus
import com.higold.cloud.showToast
import com.higold.cloud.utils.StatusBarUtil
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_user.*
import java.text.DecimalFormat

/**
 *    author : lxz
 *    date   : 2019/8/15 19:01
 *    desc   :
 */

class UserFragment : BaseFragment(), UserInfoContract.View {

    private var mTitle: String? = null
    private val mPresenter by lazy { UserInfoPresenter() }

    companion object {
        fun getInstance(title: String): UserFragment {
            val fragment = UserFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            fragment.mTitle = title
            return fragment
        }
    }

    init {
        mPresenter.attachView(this)
    }

    @SuppressLint("SetTextI18n")
    override fun setUserInfo(userInfo: UserInfo) {
        multipleStatusView.showContent()

        tv_user_name.text = userInfo.data.name
        tv_job.text = userInfo.data.level
        tv_percent.text = changeFloatToString(userInfo.data.rate * 100) + "%"

        when (userInfo.data.rate) {
            in 0..50 -> pb_plan.setLevel(0)
            in 51..100 -> pb_plan.setLevel(1)
            else -> pb_plan.setLevel(2)
        }
        pb_plan.progress = changeFloatToString(userInfo.data.rate * 100).toFloat()
        pb_plan.duration = 1500
        pb_plan.startAnim()

        tv_mission.text = "￥" + changeFloatToString(userInfo.data.salesAmount) + "w / " + "￥" + changeFloatToString(userInfo.data.missionAmount) + "w"

    }

    override fun showError(errorMsg: String, errorCode: Int) {
        showToast(errorMsg)
        if (errorCode == ErrorStatus.NETWORK_ERROR) {
            multipleStatusView.showNoNetwork()
        } else {
            multipleStatusView.showError()
        }
    }

    override fun showLoading() {
        multipleStatusView.showLoading()
    }

    override fun dismissLoading() {

    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter.detachView()
    }

    override fun getLayoutId(): Int = R.layout.fragment_user

    override fun lazyLoad() {
        mPresenter.requestUserInfo(1)
    }

    override fun initView() {
        //状态栏透明和间距处理
//        activity?.let {
//            StatusBarUtil.setPaddingSmart(it, toolbar)
//            StatusBarUtil.darkMode(it, true)
//        }
        immersionBar {
            statusBarColor(R.color.colorPrimary)
            titleBarMarginTop(fl_container)
            statusBarDarkFont(true)
            navigationBarDarkIcon(true)
        }
        tv_header_title.text = mTitle
    }

    private fun changeFloatToString(double: Double): String {
        val decimalFormat = DecimalFormat("##.###")
        return decimalFormat.format(double)
    }
}